/**
 * creation: 7-mrt-2008
 */
package org.jfonia.connect.io;

import java.io.PrintWriter;

import org.jfonia.connect.BasicNode;
import org.jfonia.connect.Value;

/**
 * Implementation of Callable which prints the value of an element
 * to a given PrintWriter (or System.out).
 * 
 * @author wijnand.schepens@gmail.com
 *
 */
public class Printer<T> extends BasicNode
{
	protected Value<T>    source;
	protected PrintWriter out;
	protected boolean     addNewline;

	public Printer(Value<T> source)
	{
		this(source, true);
	}

	public Printer(Value<T> source, boolean addNewline)
	{
		this(source, new PrintWriter(System.out), addNewline);
	}
	
	public Printer(Value<T> source, PrintWriter out, boolean addNewline)
	{
		this.source = source;
		this.out = out;
		this.addNewline = addNewline;
	}

	public boolean callThis(Object param)
	{
		if (addNewline) 
		{
			this.out.println(source.get());
			this.out.flush();
		}
		else 
			this.out.print(source.get());
		return true;
	}
}
